﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKView;

namespace YJKTextDimension.Commands
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdLabelPositionAlign : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)111;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			Transaction transaction = new Transaction(this.m_doc, "LabelPositionAlign");
			Result result;
			for (;;)
			{
				transaction.Start();
				DimensionFilter dimensionFilter = new DimensionFilter();
				IList<Element> arElement = null;
				Reference refLabel = null;
				try
				{
					arElement = this.m_selection.PickElementsByRectangle(dimensionFilter, "请框选所有要对齐的尺寸");
					refLabel = this.m_selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, dimensionFilter, "请选择基准尺寸：");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				catch (Exception ex)
				{
					string message2 = ex.Message;
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				try
				{
					this.LabelPositionAlign(arElement, refLabel);
				}
				catch (Exception ex2)
				{
					string message3 = ex2.Message;
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				transaction.Commit();
			}
			return result;
		}

		private void LabelPositionAlign(IList<Element> arElement, Reference refLabel)
		{
			Element eleBase = this.m_doc.GetElement(refLabel);
			Dimension dim = eleBase as Dimension;
			arElement.Remove((from ele in arElement
			where ele.Id == eleBase.Id
			select ele).Single<Element>());
			Line dimensionLine = this.GetDimensionLine(dim);
			foreach (Element element in arElement)
			{
				Dimension dimension = element as Dimension;
				Line dimensionLine2 = this.GetDimensionLine(dimension);
				if (GeoUtil.IsParallel(dimensionLine2.Direction, dimensionLine.Direction))
				{
					ReferenceArray referenceArray = new ReferenceArray();
					foreach (object obj in dimension.References)
					{
						Reference reference = (Reference)obj;
						Element element2 = this.m_doc.GetElement(reference);
						referenceArray.Append(new Reference(element2));
					}
					this.m_doc.Create.NewDimension(this.m_doc.ActiveView, dimensionLine, referenceArray, dimension.DimensionType);
					this.m_doc.Delete(dimension.Id);
				}
			}
		}

		private Line GetDimensionLine(Dimension dim)
		{
			double num = dim.get_Parameter(BuiltInParameter.DIM_VALUE_LENGTH).AsDouble();
			Curve curve = dim.Curve;
			Line line = curve as Line;
			XYZ origin = line.Origin;
			XYZ direction = line.Direction;
			XYZ xyz = origin - direction * num / 2.0;
			XYZ xyz2 = origin + direction * num / 2.0;
			return Line.CreateBound(xyz, xyz2);
		}

		private Document m_doc;

		private Selection m_selection;
	}
}
